package com.example.s.fragment.search;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.s.Activity.UserDetailActivity;
import com.example.s.Activity.WorksDetailActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.bean.RecContent;
import com.example.s.fragment.BaseFragment;
import com.example.s.utils.LiveDataBus;

import java.util.ArrayList;
import java.util.List;

public class HotestFragment extends BaseFragment {
    private RecyclerView rv;
    private List<RecContent> hotestList = new ArrayList<>();
    private MyRecyclerViewAdapter mHotestAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hotest;
    }

    @Override
    public void initView(View view) {

        rv = (RecyclerView) findViewById(R.id.rv);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getHotestContent();
        rv.setNestedScrollingEnabled(false);
        rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        rv.setAdapter(mHotestAdapter = new MyRecyclerViewAdapter<RecContent>(hotestList, R.layout.rec_recycler_item) {
            @Override
            public void handleData(InnerHolder holder, RecContent item) {
                holder.setSrcResource(R.id.headphoto, item.getHeadPhoto()).setSrcResource(R.id.contentPhoto,item.getContentPhoto());
                holder.setText(R.id.title, item.getTitle());
                holder.setText(R.id.subtitle, item.getSubTitle());
                holder.setText(R.id.likes, String.valueOf(item.getLikes()));
                holder.setOnClickListener(R.id.headphoto, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LiveDataBus.getInstance().with("currentUser",RecContent.class).setValue(item);
                        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
                        startActivity(intent);
                    }
                });
                holder.setOnClickListener(R.id.contentPhotoContainer, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LiveDataBus.getInstance().with("currentWorkDetail",RecContent.class).setValue(item);
                        Intent intent = new Intent(getActivity(), WorksDetailActivity.class);
                        startActivity(intent);
                    }
                });

            }
        });
    }
    private int getX(){
        int x=(int)(Math.random()*200);
        return x;
    }

    private void getHotestContent() {
        hotestList.add(new RecContent(1, R.drawable.yywtx1, "一个字就是牛", R.drawable.yyw1, "新年成为一个大牛", getX(), 666));
        hotestList.add(new RecContent(1, R.drawable.yywtx2, "居然可以这么做", R.drawable.yyw2, "满脸的问号", getX(), 666));
        hotestList.add(new RecContent(1, R.drawable.yywtx3, "笑掉大牙", R.drawable.yyw3, "笑掉大牙的操作", getX(), 666));
        hotestList.add(new RecContent(1, R.drawable.yywtx4, "不愧是我", R.drawable.yyw4, "不愧是我,居然可以这么美", getX(), 666));
        hotestList.add(new RecContent(1, R.drawable.yywtx5, "艺高人胆大", R.drawable.yyw5, "胆子大游戏里也可以冲", getX(), 666));
        hotestList.add(new RecContent(1, R.drawable.yywtx6, "快乐的亚索", R.drawable.yyw6, "面对疾风吧,哈撒给", getX(), 666));
    }
}